package com.jakesepicstuff.jnotepad;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

public class Display extends JFrame{
	
	Dimension frameSize = new Dimension(500, 500);
	Dimension textAreaSize = new Dimension(500, 500);
	String frameTitle = "JNotePad v2.0";
	
	JMenuBar menuBar;
	
	JMenu file;
	JMenuItem open, save, exit;
	
	JMenu edit;
	JMenuItem copy, paste, clear, selectAll, fontAndColor, tabSize;
	
	JMenu templates;
	JMenuItem personal ,html, java, c, cPP, cSharp, php, perl, vbNet;
	
	String personalInfoTemplate ,htmlTemplate, javaTemplate, cTemplate, cPPTemplate, cSharpTemplate, phpTemplate;
	String perlTemplate, vbNetTemplate;
	
	static JTextArea textArea;
	
	// CONSTRUCTOR
	public Display() {
		
		setTitle(frameTitle);
		setSize(frameSize);
		setLayout(new FlowLayout());
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		initComponents();
	}
	
	// INITIALIZE COMPONENTS
	public void initComponents() {
		
		menuBar = new JMenuBar();
		
		file = new JMenu("File");
		open = new JMenuItem("Open [CTRL + O]");
		save = new JMenuItem("Save [CTRL + S]");
		exit = new JMenuItem("Exit [CTRL + Q]");
		
		edit = new JMenu("Edit");
		copy = new JMenuItem("Copy [CTRL + C]");
		paste = new JMenuItem("Paste [CTRL + V]");
		clear = new JMenuItem("Clear [CTRL + R]");
		selectAll = new JMenuItem("Select All [CTRL + A]");
		fontAndColor = new JMenuItem("Font & Color [CTRL + E]");
		tabSize = new JMenuItem("Change Tab Size");
		
		templates = new JMenu("Templates");
		personal = new JMenuItem("Personal Information");
		html = new JMenuItem("HTML");
		java = new JMenuItem("Java");
		c = new JMenuItem("C");
		cPP = new JMenuItem("C++");
		cSharp = new JMenuItem("C#");
		php = new JMenuItem("PHP");
		perl = new JMenuItem("Perl");
		vbNet = new JMenuItem("VB.NET");
		
		textArea = new JTextArea();
		textArea.setPreferredSize(textAreaSize);
		textArea.setMargin(new Insets(10, 10, 10, 10));
		textArea.setLineWrap(true);
		
		htmlTemplate = "<html>\n\n<h1> Page Title </h1>\n\n<p> Page Content </p>\n\n</html>";
		
		javaTemplate = "package com.example.packagename;\n\npublic class ClassName {\n\n\tpublic static void main(String[] args) {\n\n\t}\n}";
		
		personalInfoTemplate = "Name: \n\nDOB: \n\nAddress 1:\n\nAddress 2: \n\nCity: \n\nOther Information: ";
		
		cTemplate = "#include <stdio.h>\n\nint main(void) {\n\n\treturn 0;\n}";
		
		cPPTemplate = "#include <iostream>\n\nusing namespace std;\n\nint main() {\n\n\treturn 0;\n}";
		
		cSharpTemplate = "using System;\n\npublic class Test\n{\n\tpublic static void Main()\n\t{\n\n\t}\n}";
		
		phpTemplate = "<?php\n\n?>";
		
		perlTemplate = "#!/usr/bin/perl\n";
		
		vbNetTemplate = "Imports System\n\nPublic Class Test\n\tPublic Shared Sub Main()\n\n\tEnd Sub\nEnd Class";
		
		addComponents();
	}

	// ADD COMPONENTS
	public void addComponents() {
		
		setJMenuBar(menuBar);
		menuBar.add(file);
		file.add(open);
		file.add(save);
		file.add(exit);
		
		menuBar.add(edit);
		edit.add(copy);
		edit.add(paste);
		edit.add(clear);
		edit.add(selectAll);
		edit.add(fontAndColor);
		edit.add(tabSize);
		
		menuBar.add(templates);
		templates.add(personal);
		templates.add(html);
		templates.add(java);
		templates.add(c);
		templates.add(cPP);
		templates.add(cSharp);
		templates.add(php);
		templates.add(perl);
		templates.add(vbNet);
		
		add(textArea);
		
		open.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				new OpenFile();
			}
		});
		
		save.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				new SaveFile();
			}
		});
		
		exit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				int choice = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?", "Are you sure?", JOptionPane.YES_NO_OPTION);
				if (choice == 0) {
					
					System.exit(0);
				} else {
				
					return;
				}
			}
		});
		
		copy.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				Clipboard clipboard = getToolkit().getSystemClipboard();
				StringSelection data = new StringSelection(textArea.getSelectedText());
				clipboard.setContents(data, data);
			}
		});
		
		paste.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				Clipboard clipboard = getToolkit().getSystemClipboard();
				String s;
				
				Transferable clipData = clipboard.getContents(this);
				try {
					
					s = (String) (clipData.getTransferData(DataFlavor.stringFlavor));
					textArea.append(s);
				} catch (UnsupportedFlavorException e1) {
					
					e1.printStackTrace();
				} catch (IOException e1) {
					
					e1.printStackTrace();
				}
			}
		});
		
		selectAll.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				textArea.selectAll();
			}
		});
		
		clear.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				textArea.setText("");
			}
		});
		
		textArea.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				
				
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				
				if ((arg0.getKeyCode() == KeyEvent.VK_C) && ((arg0.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
					
					Clipboard clipboard = getToolkit().getSystemClipboard();
					StringSelection data = new StringSelection(textArea.getText());
					clipboard.setContents(data, data);
					System.out.println("Text Copied");
					JOptionPane.showMessageDialog(null, "Text has been copied to clipboard");
				} else if ((arg0.getKeyCode() == KeyEvent.VK_V) && (arg0.getModifiers() & KeyEvent.CTRL_MASK) != 0) {
					
					Clipboard clipboard = getToolkit().getSystemClipboard();
					String s;
					
					Transferable clipData = clipboard.getContents(this);
					try {
						
						s = (String) (clipData.getTransferData(DataFlavor.stringFlavor));
						textArea.append(s);
					} catch (UnsupportedFlavorException e1) {
						
						e1.printStackTrace();
					} catch (IOException e1) {
						
						e1.printStackTrace();
					}
				} else if ((arg0.getKeyCode() == KeyEvent.VK_O) && ((arg0.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
					
					new OpenFile();
				} else if ((arg0.getKeyCode() == KeyEvent.VK_Q) && ((arg0.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
					
					int choice = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?", "Are you sure?", JOptionPane.YES_NO_OPTION);
					if (choice == 0) {
						
						System.exit(0);
					} else {
					
						return;
					}
				} else if ((arg0.getKeyCode() == KeyEvent.VK_R) && ((arg0.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
					
					textArea.setText("");
				} else if ((arg0.getKeyCode() == KeyEvent.VK_S) && ((arg0.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
					
					new SaveFile();
				} else if ((arg0.getKeyCode() == KeyEvent.VK_E) && ((arg0.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
					
					new FontAndColor();
				}
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				
				
			}
		});
		
		fontAndColor.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				new FontAndColor();
			}
		});
		
		tabSize.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				String tabSizeStr = JOptionPane.showInputDialog("Tab size:");
				
				int tabSizeInt = Integer.parseInt(tabSizeStr);
				
				textArea.setTabSize(tabSizeInt);
				
				System.out.printf("Tab size set to %d", tabSizeInt + "\n");
			}
		});
		
		html.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				textArea.setTabSize(4);
				textArea.setText(htmlTemplate);
			}
		});
		
		java.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				textArea.setTabSize(4);
				textArea.setText(javaTemplate);
			}
		});
		
		personal.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				textArea.setText(personalInfoTemplate);
			}
		});
		
		c.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				textArea.setTabSize(4);
				textArea.setText(cTemplate);
			}
		});
		
		cPP.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				textArea.setTabSize(4);
				textArea.setText(cPPTemplate);
			}
		});
		
		cSharp.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				textArea.setTabSize(4);
				textArea.setText(cSharpTemplate);
			}
		});
		
		php.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				textArea.setTabSize(4);
				textArea.setText(phpTemplate);
			}
		});
		
		perl.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				textArea.setTabSize(4);
				textArea.setText(perlTemplate);
			}
		});
		
		vbNet.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				textArea.setTabSize(4);
				textArea.setText(vbNetTemplate);
			}
		});
	}
}